package com.keyroy.game.puzzle_rpg.models.puzzle;

import java.awt.Point;
import java.util.List;

public class PuzzleMap {
	public Tile[][] tiles;
	protected PuzzleSet puzzleSet;

	PuzzleMap(PuzzleSet puzzleSet) {
		this.puzzleSet = puzzleSet;
		tiles = new Tile[puzzleSet.columns][puzzleSet.rows];
		reset();
	}

	public final Tile[][] copyTiles() {
		Tile[][] rTiles = new Tile[puzzleSet.columns][puzzleSet.rows];
		for (int i = 0; i < puzzleSet.columns; i++) {
			for (int j = 0; j < puzzleSet.rows; j++) {
				rTiles[i][j] = tiles[i][j].getCopy();
			}
		}
		return rTiles;
	}

	public final PuzzleSet getPuzzleSet() {
		return puzzleSet;
	}

	public final int getWidth() {
		return puzzleSet.columns * puzzleSet.gridWidth;
	}

	public final int getHeight() {
		return puzzleSet.rows * puzzleSet.gridHeight;
	}

	protected final void print() {
		print(tiles);
	}

	protected final void print(Tile[][] tiles) {
		System.out.println("------------------------------");
		for (int y = 0; y < puzzleSet.rows; y++) {
			for (int x = 0; x < puzzleSet.columns; x++) {
				Tile tile = tiles[x][y];
				if (tile != null) {
					System.out.print(tile.getID() + " ");
				} else {
					System.out.print("x ");
				}
			}

			System.out.println();
		}

	}

	public final void change(Point p1, Point p2) {
		PuzzleUtil.change(this, p1, p2);
	}

	public final void change(Result result) {
		PuzzleUtil.change(this, result.getPoint(), result.direction);
	}

	public final List<Result> find() {
		return PuzzleUtil.find(this);
	}

	public final List<Result> find(Point point) {
		return PuzzleUtil.find(this, point);
	}

	public final List<Result> tips() {
		return PuzzleUtil.tips(this);
	}

	public final void clean(List<Result> results) {
		PuzzleUtil.clean(this, results, null);
	}

	public final void dropDown() {
		PuzzleUtil.dropDown(this);
	}

	public final void fix() {
		PuzzleUtil.fix(this);
	}

	public final void reset() {
		PuzzleUtil.reset(this);
	}

	public final boolean has(Point p) {
		return has(p.x, p.y);
	}

	public final boolean has(int columnIndex, int rowIndex) {
		return columnIndex >= 0 && rowIndex >= 0 && columnIndex < puzzleSet.columns && rowIndex < puzzleSet.rows;
	}

	public final void flushVerticalTileLocation() {
		PuzzleUtil.flushVerticalTileLocation(this);
	}
}
